<?php
// +----------------------------------------------------------------------
// | Mysql Database Dict Maker Ver 1.0.0
// +----------------------------------------------------------------------
// | Copyright (c) 2016 pengyong.me All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: pengyong <i@pengyong.me>
// +----------------------------------------------------------------------
//ini_set("error_reporting","E_ALL & ~E_NOTICE");//关掉提示与警告（开始的时候可以注释掉）
error_reporting('E_ALL');
$title = 'Mysql 数据库字典生成工具 Ver 1.0.0';
$html = '<html><head><title>' . $title . '</title>';
$html .= '<style>table, td, th {
    border: 1px solid black;
};</style></head><body><div class="container">';
$html .= '<nav class="navbar navbar-default" role="navigation">';
$html .= '<div class="container-fluid"><div class="navbar-header">';
if (isset($_POST) && !empty($_POST)) {
    $host = $_POST['host'];
    $port = $_POST['port'];
    $username = $_POST['username'];
    $pass = $_POST['pass'];
    $database = $_POST['database'];
    $connection = mysqli_connect($host, $username, $pass);
    if (!$connection) {
        $html .= '<div class="alert alert-danger">数据库连接失败！</div>';
    } else {
      
        $html .= '';
        mysqli_select_db($connection, $database);
        mysqli_query($connection, 'SET NAMES utf8');
        $table_result = mysqli_query($connection, 'show tables');
        // 取得所有的表名
        while ($row = mysqli_fetch_array($table_result, MYSQLI_ASSOC)) {
            $tables[]['TABLE_NAME'] = $row['Tables_in_' . $database];
        }

        // 循环取得所有表的备注及表中列消息
        foreach ($tables as $k => $v) {
            $sql = 'SELECT * FROM ';
            $sql .= 'INFORMATION_SCHEMA.TABLES ';
            $sql .= 'WHERE ';
            $sql .= "table_name = '{$v['TABLE_NAME']}'  AND table_schema = '{$database}'";
            $table_result = mysqli_query($connection, $sql);
            while ($t = mysqli_fetch_array($table_result, MYSQLI_ASSOC)) {
                $tables[$k]['TABLE_COMMENT'] = $t['TABLE_COMMENT'];
            }
            $sql = 'SELECT * FROM ';
            $sql .= 'INFORMATION_SCHEMA.COLUMNS ';
            $sql .= 'WHERE ';
            $sql .= "table_name = '{$v['TABLE_NAME']}' AND table_schema = '{$database}'";

            $fields = array();
            $field_result = mysqli_query($connection, $sql);
            while ($t = mysqli_fetch_array($field_result, MYSQLI_ASSOC)) {
                $fields[] = $t;
            }
            $tables[$k]['COLUMN'] = $fields;
        }
       
        mysqli_close($connection);

        foreach ($tables as $k => $v) {
            $html .= '<div class="panel panel-default">';
            $html .= '<h3>'.$v['TABLE_COMMENT'].'</h3>';
            $html .= '<table class="table table-hover table-bordered" style="border-collapse: collapse;width:600">';
		   $html.='<tr><td>表名：</td><td colspan="5">'.$v['TABLE_NAME'].'</td></tr>';
			$html .= '<tr class="active"><td>字段名</td><td>数据类型</td><td>默认值</td><td>允许非空</td><td>自动递增</td><td>备注</td></tr>';
            foreach ($v['COLUMN'] as $f) {
                $html .= '<tr><td>' . $f['COLUMN_NAME'] . '</td>';
                $html .= '<td>' . $f['COLUMN_TYPE'] . '</td>';
                $html .= '<td>&nbsp;' . $f['COLUMN_DEFAULT'] . '</td>';
                $html .= '<td>&nbsp;' . $f['IS_NULLABLE'] . '</td>';
                $html .= '<td>' . ($f['EXTRA'] == 'auto_increment' ? '是' : '&nbsp;') . '</td>';
                $html .= '<td>&nbsp;' . $f['COLUMN_COMMENT'] . '</td>';
                $html .= '</tr>';
            }
            $html .= '</table>';
            $html .= '</div>';
        }
    }
} else {
    $html .= '<a class="navbar-brand" href="javascript:;">' . $title . '</a></div></div></nav>';
    $html .= '<div class="panel panel-default">';
    $html .= '<div class="panel-heading">连接数据库</div>';
    $html .= '<div class="panel-body">';
    $html .= '<form class="form" role="form" method="post" action="' . $_SERVER['SCRIPT_NAME'] . '">';
    $html .= '<div class="form-group">';
    $html .= '<label>主机地址</label>';
    $html .= '<input type="text" name="host" class="form-control" value="127.0.0.1">';
    $html .= '</div>';
    $html .= '<div class="form-group">';
    $html .= '<label>端口</label>';
    $html .= '<input type="text" name="port" class="form-control" value="3306">';
    $html .= '</div>';
    $html .= '<div class="form-group">';
    $html .= '<label>用户名</label>';
    $html .= '<input type="text" name="username" class="form-control" value="root">';
    $html .= '</div>';
    $html .= '<div class="form-group">';
    $html .= '<label>密码</label>';
    $html .= '<input type="password" name="pass" class="form-control" value="">';
    $html .= '</div>';
    $html .= '<div class="form-group">';
    $html .= '<label>数据库</label>';
    $html .= '<input type="text" name="database" class="form-control">';
    $html .= '</div>';
    $html .= '<button type="submit" class="btn btn-default">提交</button>';
    $html .= '</form>';
    $html .= '</div></div>';
}
$html .= '</div></body></html>';
echo $html;die();
